﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;

namespace S7.Windows.Customers
{
    /// <summary>
    /// Interaction logic for AddAnAccount.xaml
    /// </summary>
    public partial class AddAnAccount : Window
    {
        public AddAnAccount()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (tbUserName.Text != "" && tbPassword.Text !="" && tbFirstName.Text != "")
            {
                Customer cust = new Customer();
                cust.UserName = Info.Capitalize(tbUserName.Text);
                cust.Password = Info.Capitalize(tbPassword.Text);
                cust.FirstName = Info.Capitalize(tbFirstName.Text);
                cust.LastName = Info.Capitalize(tbLastName.Text);
                cust.Address = Info.Capitalize(tbAddress.Text);
                cust.Mobile = Info.Capitalize(tbMobile.Text);
                cust.Email = Info.Capitalize(tbEmail.Text);
                if (cbLangauges.SelectedItem != null)
                {
                    cust.Language = cbLangauges.SelectedItem.ToString();
                }
                Thread thr = new Thread(new ThreadStart(() =>
                {
                    string error = cust.SignUp();
                    if (error == "")
                    {
                        MessageBox.Show("Customer successfully signed up", "Done!", MessageBoxButton.OK, MessageBoxImage.Information);
                        this.Dispatcher.Invoke(new ThreadStart(() =>
                            {
                                this.Close();
                            }));
                    }
                    else
                    {
                        MessageBox.Show(error, "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }));
                thr.IsBackground = true;
                try
                {
                    thr.Start();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else
            {
                MessageBox.Show("Please Enter Username, Password, First Name, and select Language", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // cbLanguages =

        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
